#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
    int l = newInterval[0], r = newInterval[1];
    bool flag = false;
    vector<vector<int>> res;
    for (auto edge : intervals)
    {
        if (edge[0] > r)
        {
            if (!flag)
            {
                res.push_back({ l,r });
                flag = true;
            }
            res.push_back(edge);
        }
        else if (edge[1] < l)    res.push_back(edge);
        else
        {
            l = min(l, edge[0]);
            r = max(r, edge[1]);
        }
    }
    if (!flag)   res.push_back({ l,r });
    return res;
}